package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JLabel;
import javax.swing.JPanel;

import card.BuildingCard;
import card.Card;
import card.CreatureCard;
import card.LandCard;

/**
 * 
 * @author Anton, Oscar
 * Panel for displaying information about cards.
 */
public class CardViewPanel extends JPanel {

	private Card shownCard;
	
	private JPanel centerPanel;
	private JPanel southPanel;
	private JPanel iconPanel;
	private JPanel infoPanel;
	private JPanel namePanel;
	private JPanel statsPanel;
	private JPanel descriptionPanel;
	
	private BorderLayout mainPanelLayout;
	private BorderLayout centerPanelLayout;
	private BorderLayout infoPanelLayout;
	private BorderLayout descriptionPanelLayout;
	
	private JLabel nameLabel;
	private JLabel goldCostLabel;
	private JLabel manaCostLabel;
	private JLabel firstInfoLabel;
	private JLabel secondInfoLabel;
	private JLabel thirdInfoLabel;
	private JLabel cardDescriptionLabel;
	private JPanel cardEffectPanel;
	private JLabel cardIconLabel;
	

	public static CardViewPanel me;
	
	public static CardViewPanel getInstance(){
		if(me == null)
			me = new CardViewPanel();
		return me;
	}
	
	public CardViewPanel(){
		super();
		initCVP();
	}
	
	private void initCVP() {
		mainPanelLayout = new BorderLayout();
		setLayout(mainPanelLayout);
		
		centerPanel = new JPanel();
		southPanel = new JPanel();
		centerPanel.setOpaque(false);
		centerPanel.setPreferredSize(new Dimension(375, 140));
		centerPanel.setMinimumSize(new Dimension(375, 170));
		centerPanel.setMaximumSize(new Dimension(375, 170));
		southPanel.setOpaque(false);
		add(centerPanel, BorderLayout.NORTH);
		add(southPanel, BorderLayout.SOUTH);
		
		centerPanelLayout = new BorderLayout();
		centerPanel.setLayout(centerPanelLayout);
		
		iconPanel = new JPanel();
		infoPanel = new JPanel();
		iconPanel.setOpaque(false);
		infoPanel.setOpaque(false);
		infoPanel.setPreferredSize(new Dimension(245, 140));
		infoPanel.setMinimumSize(new Dimension(245, 170));
		infoPanel.setMaximumSize(new Dimension(245, 170));
		centerPanel.add(iconPanel, BorderLayout.WEST);
		centerPanel.add(infoPanel, BorderLayout.EAST);
		
		infoPanelLayout = new BorderLayout();
		infoPanel.setLayout(infoPanelLayout);
		
		namePanel = new JPanel();
		namePanel.setOpaque(false);
		statsPanel = new JPanel();
		statsPanel.setOpaque(false);
		statsPanel.setPreferredSize(new Dimension(245, 10));
		statsPanel.setMinimumSize(new Dimension(245, 10));
		statsPanel.setMaximumSize(new Dimension(245, 10));
		descriptionPanel = new JPanel();
		descriptionPanel.setOpaque(false);
		
		infoPanel.add(namePanel, BorderLayout.NORTH);
		infoPanel.add(statsPanel, BorderLayout.CENTER);
		infoPanel.add(descriptionPanel, BorderLayout.SOUTH);
		
		descriptionPanelLayout = new BorderLayout();
		descriptionPanel.setLayout(descriptionPanelLayout);
		
		nameLabel = new JLabel();
		nameLabel.setForeground(Color.white);
		goldCostLabel = new JLabel();
		goldCostLabel.setForeground(Color.white);
		goldCostLabel.setIcon(ImageHandler.getInstance().getImageIcon("gold.png"));
		manaCostLabel = new JLabel();
		manaCostLabel.setForeground(Color.white);
		manaCostLabel.setIcon(ImageHandler.getInstance().getImageIcon("mana.png"));
		firstInfoLabel = new JLabel();
		firstInfoLabel.setForeground(Color.white);
		secondInfoLabel = new JLabel();
		secondInfoLabel.setForeground(Color.white);
		thirdInfoLabel = new JLabel();
		thirdInfoLabel.setForeground(Color.white);
		cardDescriptionLabel = new JLabel();
		cardDescriptionLabel.setForeground(Color.white);
		cardDescriptionLabel.setPreferredSize(new Dimension(245, 70));
		cardDescriptionLabel.setVerticalAlignment(1);
		cardEffectPanel = new JPanel();
		cardIconLabel = new JLabel();
		
		iconPanel.add(cardIconLabel);
		
		namePanel.add(nameLabel);
		namePanel.add(goldCostLabel);
		namePanel.add(manaCostLabel);
		statsPanel.add(firstInfoLabel);
		statsPanel.add(secondInfoLabel);
		statsPanel.add(thirdInfoLabel);
		descriptionPanel.add(cardDescriptionLabel, BorderLayout.NORTH);
		southPanel.add(cardEffectPanel);
		cardEffectPanel.setOpaque(false);
	
		setBackground(Color.black);
		setOpaque(true);
	}

	/**
	 * Displays all information about the given Card
	 * @param c
	 */
	public void setShownCard(Card c){
		shownCard = c;
		updateInfo();
		this.repaint();
	}
	
	/**
	 * Changes the information in the CardViewPanel based on the selected card.
	 */
	public void updateInfo(){
		//Removes previous info
		if(shownCard != null){
			nameLabel.setText("");
			goldCostLabel.setText("");
			manaCostLabel.setText("");
			cardDescriptionLabel.setText("");
			cardIconLabel.setIcon(null);
			firstInfoLabel.setText("");
			secondInfoLabel.setText("");
			thirdInfoLabel.setText("");
			for(int i = 0; i < cardEffectPanel.getComponentCount(); i++)
				cardEffectPanel.remove(i);
			
			//All cards
			nameLabel.setText(shownCard.getName());
			goldCostLabel.setText(""+shownCard.getGoldCost());
			manaCostLabel.setText(""+shownCard.getManaCost());
			cardDescriptionLabel.setText(shownCard.getDescription());
			cardIconLabel.setIcon(ImageHandler.getInstance().getImageIcon(shownCard.getImageBig()));
			
			//CreatureCards
			if(shownCard instanceof CreatureCard){
				firstInfoLabel.setText("Attack:" + ((CreatureCard) shownCard).getActualAttackValueString());
				secondInfoLabel.setText("Defense:" + ((CreatureCard) shownCard).getActualDefenseString());
				thirdInfoLabel.setText("Health:" + ((CreatureCard) shownCard).getCurrentHealthString());
			}
			
			//BuildingCards
			if(shownCard instanceof BuildingCard){
				if(((BuildingCard)shownCard).getGoldIncome()!=0)
					firstInfoLabel.setText("Gold Income:" + ((BuildingCard) shownCard).getGoldIncome());
				if(((BuildingCard)shownCard).getManaIncome()!=0)
					secondInfoLabel.setText("Mana Income:" + ((BuildingCard) shownCard).getManaIncome());			
			}
			
			//LandCards
			if(shownCard instanceof LandCard){
				firstInfoLabel.setText("Gold Income:" + ((LandCard) shownCard).getGoldIncome());
				secondInfoLabel.setText("Mana Income:" + ((LandCard) shownCard).getManaIncome());
			}
			
			for(int i = 0; i < cardEffectPanel.getComponentCount(); i++)
				cardEffectPanel.remove(i);
			
			for(int i = 0; i < shownCard.getEffects().size(); i++){
				if(shownCard.getEffects().get(i).isVisible()){
					cardEffectPanel.add(new JLabel());
					JLabel temp = (JLabel)cardEffectPanel.getComponent(i);
					temp.setIcon(ImageHandler.getInstance().getImageIcon(shownCard.getEffects().get(i).getImageName()));
					temp.setToolTipText(shownCard.getEffects().get(i).getName() + " - " +
							shownCard.getEffects().get(i).getDescription());
				}
			}
		}
	}
	
	/**
	 * Method used for painting the background image.
	 */
	protected void paintComponent(Graphics g){
		
		BufferedImage img = null;
		try {
		    img = ImageIO.read(new File("images/backgroundInfoPanel1.png"));
		} catch (IOException e) {
		}
		
		g.drawImage(img , 0,0,this.getWidth(),this.getHeight(),this);
	}
}
